<?php
namespace app\home\controller;
use \think\Cookie;
use think\Db;
use \think\View;
use \think\Model;
use \think\Request;
use \GatewayClient\Gateway;
class User extends Model
{
    protected $resultSetType = 'collection';
}
class File extends Model
{
    protected $resultSetType = 'collection';
    protected $createTime = 'creat_time';
    protected $updateTime = false;
}
class Grade extends Model
{
    protected $resultSetType = 'collection';
    protected $createTime = false;
    protected $updateTime = false;
}
class StatusData extends Model
{
    protected $resultSetType = 'collection';
    protected $createTime = 'time';
    protected $updateTime = false;
}
class Sid extends Model
{
    protected $resultSetType = 'collection';
    protected $createTime = false;
    protected $updateTime = false;
}
class Comment extends Model
{
    protected $resultSetType = 'collection';
    protected $createTime = 'time';
    protected $updateTime = false;
}
class Blog extends Model
{
    protected $resultSetType = 'collection';
    protected $createTime = 'time';
    protected $updateTime = false;
}
class ChatData extends Model
{
    protected $resultSetType = 'collection';
    protected $createTime = 'time';
    protected $updateTime = false;
}
class Url extends Model
{
    protected $resultSetType = 'collection';

}

class Home extends \think\Controller
{

    public function home()    //主页
    {
        $cookie = Cookie::get('safecode');
        if (!$cookie) {
            $this->error('不好意思哦，请先登陆一下吧', '../../index/Login/login');
        }
        $data = User::getBySafecode($cookie);
        if (!$data) {
            $this->error('别改COOKIE了，赶紧去登录吧', '../../index/Login/login');
        }
        $webdate = Db::name('index')->getById(1);//根据用户识别码
        $view = new View();
        $view->assign('web', $webdate);
        $view->assign('user', $data);
        return $view->fetch('home/index', [], ['__PUBLIC__' => '/public/static']);
    }
    public function friend()     //交友中心
    {
        $friend_data = User::where('en_searched', '1')->paginate(3);
        $page = $friend_data->render();
        $view = new View();
        $view->assign('friend', $friend_data);
        $view->assign('page', $page);
        return $view->fetch('friend', [], ['__PUBLIC__' => '/public/static']);
    }
    public function loginout()   //退出
    {
        $cookie=Cookie::get('safecode');
        if(!$cookie)
        {
            $this->error('不好意思哦，请先登陆一下吧', '../../index/Login/login');
        }
        $delete = Cookie::delete('safecode');
        $this->success('已退出账户，如有需要请重新登录哦', '../../index/Login/login');
    }
    public function person()   //个人信息
    {
        $cookie = Cookie::get('safecode');
        $person_data = User::getBySafecode($cookie);
        $view = new View();
        $view->assign('user', $person_data);
        return $view->fetch('person', [], ['__PUBLIC__' => '/public/static']);
    }
    public function edit()    //个人信息修改
    {
        $person = new User();
        $status = new StatusData();
        $cookie = Cookie::get('safecode');
        $person_data = User::getBySafecode($cookie);
        if (input('post.')) {
            $post_data = input('post.');
            $update = $person->allowField(true)->save($post_data, ['safecode' => $cookie]);
            if ($update) {
                $status->insert(['msg' => '更改了自己的信息', 'uid' => $person_data['Id'], 'time' => date('Y-m-d H:i:s', time())]);
                $this->success('操作成功', 'edit');
            } else {
                $this->error('什么都没有发生...');
            }
        }

        $view = new View();
        $view->assign('person', $person_data);
        return $view->fetch('edit', [], ['__PUBLIC__' => '/public/static']);
    }
    public  function user_data()    //用户信息
    {
        $cookie = Cookie::get('safecode');
        if (input('get.name')) {
            $name = input('get.name');
            $friend_data = User::getByName($name)->toArray();
        } elseif (input('get.Id')) {
            $Id = input('get.Id');
            $friend_data = User::getById($Id)->toArray();
        }
        $user = User::getBySafecode($cookie);
        $status = new StatusData();
        if ($user['Id'] != $friend_data['Id']) {
            $status->insert(['msg' => '访问了' . $friend_data['name'] . '的空间', 'uid' => $user['Id'], 'time' => date('Y-m-d H:i:s', time())]);
        }
        $status_data = $status->where('uid', $friend_data['Id'])->order('time', 'desc')->limit(10)->select()->toArray();
        $view = new View();
        $view->assign('status', $status_data);
        $view->assign('friend', $friend_data);
        return $view->fetch('user_data', [], ['__PUBLIC__' => '/public/static']);
    }
}